package com.fujitsu.hope.web.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fujitsu.hope.web.client.bean.Irc;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class IrcEntryPoint implements EntryPoint {

	@SuppressWarnings("unused")
	private final Messages messages = GWT.create(Messages.class);
	
	private TableBinder binder;
	
	private DateTimeFormat formatter = DateTimeFormat.getFormat("yyyyMMdd");
	
	private DockLayoutPanel dockLayoutPanel;
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final IrcServiceAsync service = GWT.create(IrcService.class);
	
	private void query(String arg){
		final PopupPanel popup = new PopupPanel(false, true);
		Label popupLabel = new Label("Please wait ... ");
		popup.add(popupLabel);
		popup.setGlassEnabled(true);
		popup.center();
		service.queryMessage(arg, new AsyncCallback<List<Irc>>() {
			public void onFailure(Throwable caught) {
				popup.hide();
			}
			public void onSuccess(List<Irc> result) {
				binder.bind(result);
				popup.hide();
			}
		});
	}
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		FlexTable table = new FlexTable();
		table.setSize("100%", "100%");
		binder = new TableBinder(table);
		binder.bind(initList());
		dockLayoutPanel = new DockLayoutPanel(Unit.EM);
		dockLayoutPanel.setSize("100%", "100%");
		dockLayoutPanel.addNorth(searcherWidget(), 8);
		dockLayoutPanel.addWest(navigatorWidget(), 10);
		ScrollPanel sp = new ScrollPanel(table);
		dockLayoutPanel.add(sp);
		RootLayoutPanel.get().add(dockLayoutPanel);
	}
	
	private List<Irc> initList(){
		List<Irc> result = new ArrayList<Irc>();
 		for(int i=100; i<110; i++){
 			result.add(dummy(i));
		}
 		return result;
	}
	
	private Irc dummy(int id){
		Irc bean = new Irc();
		bean.setId((long)id);
		bean.setChannel("#Ch"+id%2);
		bean.setNickname("user"+id%10);
		bean.setMessage("message"+id%3);
		Date date = new Date((long)(new Date().getTime())+3600000*id);
		bean.setTime(date);
		return bean;
	}
	
	Widget navigatorWidget(){
		VerticalPanel panel = new VerticalPanel();
		panel.add(new Hyperlink("channel", "all"));
		panel.add(new Hyperlink("channel", "abc"));
		panel.add(new Hyperlink("channel", "def"));
		panel.add(new Hyperlink("channel", "hig"));
		panel.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
		return panel;
	}
	
	Widget searcherWidget(){
		final Label title = new Label();
		title.setText("IRC search");
		title.setStyleName("titleLogo");
		
		final TextBox textBox = new TextBox();
		textBox.setWidth("300 px");
		
		String channel = Window.Location.getParameter("channel");
		if(channel != null) 
			textBox.setText(channel);
		
		final Button button = new Button("Search");
		ClickHandler handler = new ClickHandler() {
			public void onClick(ClickEvent event) {
				query(textBox.getText());
			}
		};
		button.addClickHandler(handler);
		
		FlowPanel panel = new FlowPanel();
		panel.add(title);
		panel.add(textBox);
		panel.add(button);
		FormPanel form = new FormPanel();
		form.setWidget(panel);
		return form;
	}

	private class TableBinder{
		private FlexTable table;
		TableBinder(FlexTable table){
			this.table = table;
		}
		
		void bind(List<Irc> data){
			table.removeAllRows();
			for(int row=0; row<data.size(); row++){
				Irc bean = data.get(row);
				String dateStr = formatter.format(bean.getTime());
				table.setWidget(row, 0, createWidget(dateStr));
				table.setWidget(row, 1, createWidget(bean.getNickname()));
				table.setWidget(row, 2, createWidget("#"+bean.getChannel()));
				table.setWidget(row, 3, new Label(bean.getMessage()));
			}
		}
		
		Widget createWidget(String text){
			Label label = new Label();	
			label.setText(text);
			label.setStyleName("messageHeader");
			return label;
		}
	}
}